package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.partrecord;

import java.util.List;

import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.MaintainWorkDetailBean;
import st.business.tepth.business.bean.PartLocationBean;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-07-01 14:50
 */

public class PartRecordPresenter extends PartRecordContract.Presenter {
    private final PartRecordContract.View mView;
    private final PartRecordContract.Model mModel;

    public PartRecordPresenter(PartRecordContract.View view) {
        mView = view;
        mModel = new PartRecordModel();
    }

    @Override
    void getDetailData(String token, int maintenanceId) {
        addDisposable(mModel.getDetailData(token, maintenanceId,
                new OnDataConvertListener<MaintainWorkDetailBean>() {
                    @Override
                    public void convert(MaintainWorkDetailBean workBean) {
                        mView.showData(workBean);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void confirmProgram(String token, int repairId, int repairTaskId, int maintainId, String taskItemList) {
        addDisposable(mModel.postProgram(token, repairId,
                repairTaskId, maintainId, taskItemList,
                new OnDataConvertListener<MaintainWorkDetailBean>() {
                    @Override
                    public void convert(MaintainWorkDetailBean workBean) {
                        mView.showData(workBean);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void deleteProgram(String token, int repairId, int faultDiagnosticId, int planId, final int position) {
        addDisposable(mModel.deleteProgram(token, repairId, faultDiagnosticId, planId, new OnDataConvertListener<String>() {
            @Override
            public void convert(String s) {
                mView.deleteProgram(position);
            }

            @Override
            public void error(int code, String msg) {
                mView.showError(msg);
            }
        }));
    }
    @Override
    void addTempLocation(String token, final int position,
                         final PartLocationBean partLocationBean, String tempLocation) {
        addDisposable(mModel.addTempLocation(token, tempLocation,
                new OnDataConvertListener<List<PartLocationBean>>() {
                    @Override
                    public void convert(List<PartLocationBean> locationBeans) {
                        PartLocationBean partLocationBeanNew = partLocationBean;
                        if (partLocationBean == null) {
                            partLocationBeanNew = new PartLocationBean();
                        }
                        join(locationBeans, partLocationBeanNew);
                        mView.showTempLocation(partLocationBeanNew, position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    private void join(List<PartLocationBean> list, PartLocationBean newP) {
        if (list != null) {
            String name = newP.getPositionName() == null ? "" : newP.getPositionName()+",";
            String code = newP.getPositionCode() == null ? "" : newP.getPositionCode()+",";

            for (PartLocationBean partLocationBean : list) {
                name += partLocationBean.getPositionName() + ",";
                code += partLocationBean.getPositionCode() + ",";
            }
            name = name.substring(0, name.length() - 1);
            code = code.substring(0, code.length() - 1);
            newP.setPositionName(name);
            newP.setPositionCode(code);
        }
    }
}
